﻿using NET6.WebAPI.Attributes;
using System.ComponentModel;

namespace NET6.WebAPI.Extensions
{
    public static class EnumExtension
    {
        /// <summary>
        /// Enum 转 Int
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToInt(this Enum? value)
        {
            if (value != null)
            {
                return Convert.ToInt32(value);
            }

            return 0;
        }

        /// <summary>
        /// 获取枚举 Description 特性值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum? value)
        {
            if (value != null)
            {
                return value.GetFieldInfo()
                    .GetAttributeValue<DescriptionAttribute, string>(attr => attr.Description);
            }

            return "";
        }

        /// <summary>
        /// 获取枚举 BoolValue 特性值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool GetBoolValue(this Enum? value)
        {
            if (value != null)
            {
                return value.GetFieldInfo()
                    .GetAttributeValue<BoolValueAttribute, bool>(attr => attr.Value);
            }

            return false;
        }
    }
}
